function [polar, polar_hc] = GeneratePolarMap(params, max, varargin)
% Generate a polar map
% params.th: preferred angle obtained by vector averaging, normalized to 0-1 
%       (result of e.g. CalcMapParams).
% params.mag: vector magnitude
% max: maximum value of dF images
%
% optional parameters
% 'ColorShift', value
% 'OutputPath', 'pathname' (in this case angle map will be saved)
%
% output is polar map whereas
% hue: preferred angle
% intensity: vector magnitude
%
% polar_hc: high contrast polar map

colorShift = 60/360; % this gives blue as horizontal orientation, assuming that the hue_table starts with red by default
pathname = [];
highcontrast = 0;

% process optional parameters
if ~isempty(varargin)
    numIndex = find(cellfun('isclass', varargin(1:end-1), 'char'));
    for ind = 1:length(numIndex)
        switch lower(varargin{numIndex(ind)})
            case 'colorshift'
                colorShift = varargin{numIndex(ind) + 1};
            case 'outputpath'
                pathname = varargin{numIndex(ind) + 1};
        end
    end
end

dim = size(params.th);
polar = zeros(dim(1),dim(2),3);
polar_hc = zeros(dim(1),dim(2),3); 

params.th = mod(params.th + colorShift, 1 );
for x = 1:dim(1)
    for y = 1:dim(2)
        m1 = params.mag(x,y) ./ max;
        m2 = params.mag(x,y) * 2 ./ max;
        if m1 > 1; m1 = 1; end;
        if m2 > 1; m2 = 1; end;
        polar(x,y,:) = HsvToRgb(params.th(x,y),1,m1)';
        polar_hc(x, y, :) = HsvToRgb(params.th(x,y),1,m2)'; %ML: changed to get same color as in cell maps
%         polar(x,y,:)=hsv2rgb([params.th(x,y),1,m1]);
%         polar_hc(x,y,:)=hsv2rgb([params.th(x,y),1,m2]);           
    end
end

if ~isempty(pathname)
    imwrite(polar, fullfile(pathname, 'polar.tif'));
    imwrite(polar_hc, fullfile(pathname, 'polar_hc.tif'));
end

